package com.example.spingbootvue.controller;

import com.example.spingbootvue.entity.Resumemsg;
import com.example.spingbootvue.entity.User;
import com.example.spingbootvue.entity.employeemessage;
import com.example.spingbootvue.mapper.UserMapper;
import com.example.spingbootvue.mapper.employeemessageMapper;
import com.example.spingbootvue.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/user")  //一级路由
public class Usercontroller {

    private String username;
    @Autowired
    UserMapper userMapper;
    @Autowired//单独添加注解
    employeemessageMapper employeemessageMapper;

    @Autowired
    loginservice loginservice;

    @Autowired
    editmsgservice editmsgservice;

    @Autowired
    registerservice registerservice;

    @Autowired
    deleteservice deleteservice;

    @Autowired
    limitservice limitservice;

    @Autowired
    handinresumeservice handinresumeservice;

    @Autowired
    communityservice communityservice;

    //查询User实体类所有属性
    @GetMapping("/all") //二级路由
    public List<User> selectAll() {
      return userMapper.selectAll();
    }

    //查询employeemeaasge实体类所有属性
    @GetMapping("/findall")//二级路由
    public List<employeemessage> findAll(){
     return employeemessageMapper.findAll();
    }

    //查询用户投简历的记录
    @GetMapping("/findresume")
    public List<employeemessage> findresume(@RequestParam String account){
        return employeemessageMapper.findresume(account);
    }

    //申请记录界面用户点击删除记录即可删除对应的申请工作的记录
    @PutMapping("/updatebyaccount/{id}")
    public int updatebyaccount(@PathVariable  Integer id){
        return employeemessageMapper.updateByaccount(id);
    }

    //前端雇佣员工信息页面(employee)信息传入数据库
    @PostMapping("/employee/{account}")
    public int insertAll(@RequestBody employeemessage employee,@PathVariable String account){
        return employeemessageMapper.insertAll(employee.getFirmname(),employee.getPosition(),employee.getWage(),employee.getContacts(),employee.getPhone(),employee.getFirmaddress(),account);
    }

    //前端求职点击投简历在数据库中插入信息
    @PostMapping("/ifresume")
    public int resume(@RequestBody employeemessage employeemessage,@RequestParam Integer id,@RequestParam String applicantaccount,@RequestParam String applicant,@RequestParam String educational){
        return handinresumeservice.ifresume(employeemessage,id,applicantaccount,applicant,educational);
    }


    //前端user-manager页面编辑账号信息同步数据库更新数据
    @PutMapping("/update/{id}")
    //传入的是User实体类,默认所有的属性都会传进去
    public int updateAll(@RequestBody User user)
    {
        return editmsgservice.updatemsg(user);
    }

    //登录界面查询用户账号和密码实现登录
    @PostMapping ("/logintestnp")
    public User testlogin(@RequestBody User user) {
            System.out.println("调用了/logintestnp接口");
           return loginservice.logintest(user);
    }

    //用户管理界面点击删除按钮能够将用户的信息删除
    @DeleteMapping("/delete/{id}")
    public int deleteALL(@PathVariable Integer id){
        return deleteservice.delete(id);
        }

    //权限管理界面管理员为用户降权
    @PutMapping("/relimit/{id}")
    public Integer downpower(@PathVariable Integer id){
         return  limitservice.relimit(id);
    }

    //权限管理管理员将普通用户升级为管理员
    @PutMapping("/uplimit/{id}")
    public Integer uppower(@PathVariable Integer id){
        return  limitservice.uplimit(id);
    }

    //权限管理管理员将用户封禁,用户无法登录
    @PutMapping("/banaccount/{id}")
    public Integer banaccount(@PathVariable Integer id){
        return limitservice.isban(id);
    }

    //权限管理管理员将用户解封,使用户能够正常登录
    @PutMapping("/removeaccount/{id}")
    public Integer removeaccount(@PathVariable Integer id){
        return limitservice.noban(id);
    }

    //上传图片传入url地址
    @PutMapping("/uplodecode/{account}")
    public int uplodecode(@RequestBody Resumemsg resumemsg, @PathVariable String account){
        return userMapper.insertcode(resumemsg.getImage(),account);
    }

    //将用户个人简历上传的图片显示在前端
    @GetMapping("/image/{account}")
    public Resumemsg selectcode(@PathVariable String account){
        return userMapper.selectcode(account);
    }

    //删除个人简介的图片
    @PutMapping("/deletecode/{account}")
    public int deletecode(@PathVariable String account){
        return userMapper.deletecode(account);
    }

    //登录时随机生成图片作为社区头像,数据库更新数据
    @PutMapping("/userimage")
    public int userimage(@RequestBody User user){
        int ts=communityservice.queryImage(user.getAccount(),user.getImage());
        if(ts == 1){
            return 1;
        }else {
            return userMapper.updateimage(user.getAccount(),user.getImage());
        }
    }

    //登录时随机生成昵称作为社区昵称,数据库更新数据
    @PutMapping("/SickName")
    public int sickname(@RequestBody User user){
        int ts=communityservice.querySickName(user.getAccount(),user.getSickname());
        if(ts == 1){
            return 1;
        }else {
            return userMapper.updateSickName(user.getAccount(),user.getSickname());
        }
    }

    //将昵称和随机图片等信息查询出来
    @GetMapping("/selectByaccount/{account}")
    public User selectByaccount(@PathVariable String account){
        return userMapper.selectByaccount(account);
    }
}
